import 'dart:async';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'base_states.dart';

/// 详情数据加载基类
abstract class BaseDetailNotifier<T> extends StateNotifier<DetailState<T>> {
  BaseDetailNotifier() : super(const DetailState.initial());

  /// 自动加载数据
  bool get autoLoad => true;

  /// 加载数据抽象方法
  Future<T> loadData();

  /// 初始化加载
  Future<void> initLoad() async {
    if (!autoLoad || state.isLoading) return;

    state = DetailState.loading(data: state.data);

    try {
      final data = await loadData();
      state = DetailState.loaded(data: data);
      onLoadSuccess(data);
    } catch (e) {
      state = DetailState.error(
        error: e.toString(),
        data: state.data,
      );
      onErrorOccurred(e);
    }
  }

  /// 刷新数据
  Future<void> refresh() async {
    if (state.isRefreshing) return;

    state = DetailState.refreshing(data: state.data);

    try {
      final data = await loadData();
      state = DetailState.loaded(data: data);
      onRefreshSuccess(data);
    } catch (e) {
      state = state.copyWith(
        state: state.hasData ? LoadState.loaded : LoadState.error,
        error: e.toString(),
        isRefreshing: false,
      );
      onErrorOccurred(e);
    }
  }

  /// 重试
  Future<void> retry() async {
    await initLoad();
  }

  /// 更新数据
  void updateData(T data) {
    state = DetailState.loaded(data: data);
    onUpdateSuccess(data);
  }

  /// 清空数据
  void clear() {
    state = const DetailState.initial();
  }

  /// 加载成功回调
  void onLoadSuccess(T data) {
    // 子类可以重写
  }

  /// 刷新成功回调
  void onRefreshSuccess(T data) {
    // 子类可以重写
  }

  /// 更新成功回调
  void onUpdateSuccess(T data) {
    // 子类可以重写
  }

  /// 错误回调
  void onErrorOccurred(dynamic error) {
    // 子类可以重写处理错误
  }
}

/// 带参数的详情数据加载基类
abstract class BaseDetailParamNotifier<T, P> extends StateNotifier<DetailState<T>> {
  final P param;

  BaseDetailParamNotifier(this.param) : super(const DetailState.initial());

  /// 自动加载数据
  bool get autoLoad => true;

  /// 加载数据抽象方法
  Future<T> loadData(P param);

  /// 初始化加载
  Future<void> initLoad() async {
    if (!autoLoad || state.isLoading) return;

    state = DetailState.loading(data: state.data);

    try {
      final data = await loadData(param);
      state = DetailState.loaded(data: data);
      onLoadSuccess(data);
    } catch (e) {
      state = DetailState.error(
        error: e.toString(),
        data: state.data,
      );
      onErrorOccurred(e);
    }
  }

  /// 刷新数据
  Future<void> refresh() async {
    if (state.isRefreshing) return;

    state = DetailState.refreshing(data: state.data);

    try {
      final data = await loadData(param);
      state = DetailState.loaded(data: data);
      onRefreshSuccess(data);
    } catch (e) {
      state = state.copyWith(
        state: state.hasData ? LoadState.loaded : LoadState.error,
        error: e.toString(),
        isRefreshing: false,
      );
      onErrorOccurred(e);
    }
  }

  /// 重试
  Future<void> retry() async {
    await initLoad();
  }

  /// 更新数据
  void updateData(T data) {
    state = DetailState.loaded(data: data);
    onUpdateSuccess(data);
  }

  /// 清空数据
  void clear() {
    state = const DetailState.initial();
  }

  /// 加载成功回调
  void onLoadSuccess(T data) {
    // 子类可以重写
  }

  /// 刷新成功回调
  void onRefreshSuccess(T data) {
    // 子类可以重写
  }

  /// 更新成功回调
  void onUpdateSuccess(T data) {
    // 子类可以重写
  }

  /// 错误回调
  void onErrorOccurred(dynamic error) {
    // 子类可以重写处理错误
  }
}

/// 表单数据Notifier基类
abstract class BaseFormNotifier<T> extends StateNotifier<DetailState<T>> {
  BaseFormNotifier() : super(const DetailState.initial());

  /// 提交表单数据抽象方法
  Future<T> submitData(T data);

  /// 验证表单数据抽象方法
  Future<bool> validateData(T data);

  /// 提交表单
  Future<bool> submit(T data) async {
    if (state.isLoading) return false;

    try {
      // 先验证数据
      final isValid = await validateData(data);
      if (!isValid) {
        state = const DetailState.error(error: '表单数据验证失败');
        return false;
      }

      state = DetailState.loading(data: state.data);

      final result = await submitData(data);
      state = DetailState.loaded(data: result);
      onSubmitSuccess(result);
      return true;
    } catch (e) {
      state = DetailState.error(
        error: e.toString(),
        data: state.data,
      );
      onErrorOccurred(e);
      return false;
    }
  }

  /// 更新表单数据
  void updateFormData(T data) {
    state = DetailState.loaded(data: data);
  }

  /// 清空表单
  void clearForm() {
    state = const DetailState.initial();
  }

  /// 提交成功回调
  void onSubmitSuccess(T data) {
    // 子类可以重写
  }

  /// 错误回调
  void onErrorOccurred(dynamic error) {
    // 子类可以重写处理错误
  }
}

/// 异步操作Notifier基类
abstract class BaseAsyncNotifier<T> extends StateNotifier<DetailState<T>> {
  BaseAsyncNotifier() : super(const DetailState.initial());

  /// 执行异步操作
  Future<bool> execute(Future<T> Function() operation) async {
    if (state.isLoading) return false;

    state = DetailState.loading(data: state.data);

    try {
      final result = await operation();
      state = DetailState.loaded(data: result);
      onSuccess(result);
      return true;
    } catch (e) {
      state = DetailState.error(
        error: e.toString(),
        data: state.data,
      );
      onErrorOccurred(e);
      return false;
    }
  }

  /// 执行异步操作(无返回值)
  Future<bool> executeVoid(Future<void> Function() operation) async {
    if (state.isLoading) return false;

    state = DetailState.loading(data: state.data);

    try {
      await operation();
      state = state.copyWith(
        state: LoadState.loaded,
        clearError: true,
      );
      onVoidSuccess();
      return true;
    } catch (e) {
      state = DetailState.error(
        error: e.toString(),
        data: state.data,
      );
      onErrorOccurred(e);
      return false;
    }
  }

  /// 成功回调
  void onSuccess(T data) {
    // 子类可以重写
  }

  /// 无返回值成功回调
  void onVoidSuccess() {
    // 子类可以重写
  }

  /// 错误回调
  void onErrorOccurred(dynamic error) {
    // 子类可以重写处理错误
  }
}